import os.path
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

path = os.path.join(os.path.abspath('html'),'frame.html')
driver = webdriver.Chrome()
driver.get(path)

# 找不到id为inner_h3的元素? 必须切换到对应的iframe才能定位
# inner_h3 = driver.find_element(By.ID, 'inner_h3')
# print(inner_h3.get_attribute('outerHTML'))


# 切换到id为f1的iframe
f1 = driver.find_element(By.ID, 'f1')
driver.switch_to.frame(f1)
inner_h3 = driver.find_element(By.ID, 'inner_h3')
print(inner_h3.get_attribute('outerHTML'))

# 切换到id为f2的iframe(有嵌套的情况下,要一层一层往下找)
f2 = driver.find_element(By.ID, 'f2')
driver.switch_to.frame(f2)
p1 = driver.find_element(By.ID, 'p1')
print(p1.text)

# 切换到父级frame f1
# driver.switch_to.parent_frame()
# inner_h3 = driver.find_element(By.ID, 'inner_h3')
# print(inner_h3.text)

# 切换到主页面
driver.switch_to.default_content()
h3 = driver.find_element(By.XPATH, '//h3')
print(h3.text)


time.sleep(2)
driver.quit()